﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace CryptoFiles.Managers
{
    public static class FileManager
    {
        private static string _usersDir = Path.Combine(HttpContext.Current.Server.MapPath("~/"), "users"); 

        public static void CreateUserDir(string login)
        {
            DirectoryInfo di = new DirectoryInfo(_usersDir);
            di.CreateSubdirectory(login);
        }

        public static void SaveFile(CryptoFileInfo fileInfo)
        {
            string path = Path.Combine(Path.Combine(_usersDir, fileInfo.Login), fileInfo.FileName);

            File.WriteAllBytes(path, fileInfo.FileContent);
        }

        public static string[] GetUserFileList(string login)
        {
            string userDir = Path.Combine(_usersDir, login);

            string[] files = Directory.GetFiles(userDir);

            return files.Select(Path.GetFileName).OrderBy(o => o).ToArray();
        }

        public static byte[] GetFile(string login, string fileName)
        {
            string userDir = Path.Combine(_usersDir, login);

            string path = Path.Combine(userDir, fileName);


            byte[] encryptedFile = File.ReadAllBytes(path);


            return encryptedFile;

        }
    }
}
